package com.liuyanzhao.ssm.blog.controller.admin;

import com.liuyanzhao.ssm.blog.entity.Parameter;
import com.liuyanzhao.ssm.blog.service.ParameterService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lixiang
 */
@Controller
public class ParameterController {

   @Autowired
   private ParameterService parameterService;

    @RequestMapping("/admin/article")
    public ModelAndView test() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/Admin/Parameter/test");
        return modelAndView;
    }

    @RequestMapping("/admin/article/update")
    public ModelAndView update() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/Admin/Parameter/update");
        return modelAndView;
    }

    @RequestMapping("/admin/article/select")
    public ModelAndView select() {
        ModelAndView modelAndView = new ModelAndView();
        List<Parameter> list = new ArrayList<>();
        list =  parameterService.getParameter();
        modelAndView.setViewName("/Admin/Parameter/select");
        modelAndView.addObject("list",list);
        return modelAndView;
    }

    @RequestMapping(value = "/addParameter", method = RequestMethod.POST, produces = {"text/plain;charset=UTF-8"})
    @ResponseBody
    public String addParameter(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        String Indexes = request.getParameter("Indexes");
        String parameterName = request.getParameter("parameterName");
        String type = request.getParameter("type");
        String instructions = request.getParameter("instructions");
        Parameter parameter =new Parameter();
        parameter.setParameterName(parameterName);
        parameter.setIndexes(Indexes);
        parameter.setType(type);
        parameter.setInstructions(instructions);
        try {
            Boolean b = parameterService.addParameter(parameter);
            if (b){
                map.put("code",0);
                map.put("msg","插入成功！");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        String result = new JSONObject(map).toString();
        return result;
    }

    @RequestMapping(value = "/GetParameter")
    @ResponseBody
    public String GetParameter() {
        List<Parameter> list = new ArrayList<>();
        list =  parameterService.getParameter();
        Map<String,List> map =new HashMap<>();
        String result = new JSONObject(map).toString();
        return result;
    }

    @RequestMapping(value = "/selectParameter")
    @ResponseBody
    public Map<String,List> selectParameter() {
        List<Parameter> list = new ArrayList<>();
        list =  parameterService.getParameter();
        Map<String,List> map =new HashMap<>();
        map.put("data",list);
        return map;
    }



}
